import java.io.IOException;

import javax.microedition.lcdui.Image;

public class Player {
	private boolean is_me;
	private char symbol;
	private Image image;
	private String name;
	private TicTacToe game;

	public Player(boolean me, char symb, TicTacToe g) {
		is_me = me;
		symbol = symb;
		game = g;
		try {
			image = Image.createImage("/" + symb + "_"
					+ (is_me ? "mine" : "theirs") + ".png");
		} catch (IOException e) {
			System.out.println("Could not find system images!");
			System.exit(0);
		}
	}

	public Image getImage() {
		return image;
	}

	public char getSymbol() {
		return symbol;
	}

	public boolean isMe() {
		return is_me;
	}

	public void setName(String n) {
		name = n;
	}

	public String getName() {
		return name;
	}

	public boolean isWinner() {
		if (isWinnerHelper(0, 1, 2) || isWinnerHelper(3, 4, 5)
				|| isWinnerHelper(6, 7, 8) || isWinnerHelper(0, 3, 6)
				|| isWinnerHelper(1, 4, 7) || isWinnerHelper(2, 5, 8)
				|| isWinnerHelper(0, 4, 8) || isWinnerHelper(2, 4, 6))
			return true;
		return false;
	}

	public boolean isWinnerHelper(int a, int b, int c) {
		GameBoard board = game.board;
		if (board.grid[a] == null || board.grid[b] == null
				|| board.grid[c] == null)
			return false;
		if (board.grid[a].isMe() == is_me && board.grid[b].isMe() == is_me
				&& board.grid[c].isMe() == is_me)
			return true;
		return false;
	}
}